package com.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取地址的     百度地址api
 */
public class AreaUtil {
    public static Q returnAddressCollections() {
        String url = "https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=28209&from_mid=1&&format=json&query=%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E4%B8%8B%E8%BE%96%E7%9C%81%E7%BA%A7%E8%A1%8C%E6%94%BF%E5%8C%BA&pn=0&rn=70";
        String data = getUrl(url);
        JSONArray data1 = JSONObject.parseObject(data).getJSONArray("data");
        JSONArray result = JSONObject.parseObject(data1.get(0).toString()).getJSONArray("result");
        Map<String, String> map = new HashMap<>();

        for (Object da : result) {
            JSONObject jsonObject1 = JSONObject.parseObject(da.toString());
            String ename = (String) jsonObject1.get("ename");
            String body;
            //省
            if (ename.substring(ename.length() - 1, ename.length()).equals("省") ||
                    ename.substring(ename.length() - 3, ename.length()).equals("自治区")) {
                String province = "https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=28209&from_mid=1&&format=json&ie=utf-8&oe=utf-8&query=" + ename + "%E6%9C%89%E5%A4%9A%E5%B0%91%E4%B8%AA%E5%9F%8E%E5%B8%82&sort_key=&sort_type=1&stat0=&stat1=&stat2=&stat3=&pn=0&rn=100";
                body = getUrl(province);
            } else {
                String administrativeArea = "https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=28209&from_mid=1&&format=json&ie=utf-8&oe=utf-8&query=" + ename + "%E6%9C%89%E5%A4%9A%E5%B0%91%E4%B8%AA%E5%8C%BA&sort_key=&sort_type=1&stat0=&stat1=&stat2=&stat3=&pn=0&rn=100";
                body = getUrl(administrativeArea);
            }
            map.put(ename, body);
        }

        //提取城市和区
        Map<String, List<String>> datas = new HashMap<>();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String rp = entry.getValue();
            List<String> list = new ArrayList<>();
            JSONArray dataRp = JSONObject.parseObject(rp).getJSONArray("data");
            if(StringUtils.isEmpty(dataRp.get(0).toString())){
                continue;
            }
            JSONArray dataRpList = JSONObject.parseObject(dataRp.get(0).toString()).getJSONArray("result");
            for (Object datarPs : dataRpList) {
                // System.out.println(datarPs);
                //拿到可能就是市还不是区
                String ename = (String) JSONObject.parseObject(datarPs.toString()).get("ename");
                list.add(ename);
            }
            datas.put(entry.getKey(), list);
        }
        return Q.ok().put("data", datas);
    }

    private static String getUrl(String url) {
        HttpGet httpGet = new HttpGet(url);
        HttpClient client = new DefaultHttpClient();
        HttpResponse execute = null;
        try {
            execute = client.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = execute.getEntity();
        String data = null;
        try {
            data = unicodeToString(EntityUtils.toString(entity, "utf8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }


    private static String unicodeToString(String str) {

        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
    }
}
